﻿using Core.DataAccess.Model;
using Core.DataAccess.Model.iTool.Logger;
using Core.Framework.Common;
using Log.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Log.Implement
{
    public class LogClientExceptions : ILogClientException
    {
        public async Task<List<LogClientException>> GetClients(Pagination pagination, string searchValue, DateTime? startdate, DateTime? enddate)
        {
            await using (iToolLoggerContext context = new iToolLoggerContext())
            {
                Expression<Func<LogClientException, bool>> lamdba = (a) => a.Id > 0;
                if (!string.IsNullOrEmpty(searchValue))
                {
                    lamdba = lamdba.And(a => a.Category.Contains(searchValue) || a.Level.Contains(searchValue));

                }
                if (startdate.HasValue)
                {
                    lamdba = lamdba.And(a => a.CreateTime >= startdate.Value);
                }
                if (enddate.HasValue)
                {
                    lamdba = lamdba.And(a => a.CreateTime <= enddate.Value);
                }
                var result = context
                               .LogClientExceptions.Where(lamdba).OrderByDescending(a => a.CreateTime)
                               .PaginationAsync(pagination);
                return await result;
            }
        }
    }
}
